import random
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

import openpyxl
import yaml
from selenium import webdriver
from selenium.webdriver.support.select import Select


class BoxDriver:

    def __init__(self, broswer='Chrome') -> None:
        if broswer == 'Chrome':
            self.driver = webdriver.Chrome()
        elif broswer == 'Firefox':
            self.driver = webdriver.Firefox()
        elif broswer == 'Edge':
            self.driver = webdriver.Edge()
        elif broswer == 'Opera':
            self.driver = webdriver.Opera()
        elif broswer == 'Safari':
            self.driver = webdriver.Safari()
        elif broswer == 'Ie':
            self.driver = webdriver.Ie()
        self.implicitly_wait(10)
        self.driver.get("http://192.168.152.5:10086/ranzhi/sys/user-login-L3JhbnpoaS9zeXMv.html")

    def get(self, url):
        """  
        打开指定的页面
        url     页面地址
        """
        self.driver.get(url)

    def maximize_window(self):
        """
        窗口最大化
        """
        self.driver.maximize_window()

    def implicitly_wait(self, seconds):
        """
        隐式等待
        seconds     最大等待时间，单位是秒
        """
        self.driver.implicitly_wait(seconds)

    def input(self, by, value, text):
        """  
        向元素中写入内容
        by      定位方式
        value   定位值
        text    写入的内容
        """
        inputs = self.driver.find_elements(by, value)
        if len(inputs) > 0 :
            return inputs[0].send_keys(text)
        else :
            return ""

    def click(self, by, value):
        """  
        单击元素
        by      定位方式
        value   定位值
        """
        self.driver.find_element(by, value).click()

    def switch_to_frame(self, by, value):
        """  
        进入iframe元素
        """
        iframe = self.driver.find_element(by, value)
        self.driver.switch_to.frame(iframe)

    def switch_to_frame_index(self, index):
        """
        进入iframe元素
        """
        self.driver.switch_to.frame(index)

    def switch_to_parent_frame(self):

        self.driver.switch_to.parent_frame()

    def select_by_index(self, by, by_value, index):
        """  
        根据index进行选择
        by      定位方式
        by_value   定位值   
        index      下标    
        """
        select = self.driver.find_element(by, by_value)
        depts = Select(select)
        depts.select_by_index(index)  # 根据下标选择，下标从0开始

    def select_by_value(self, by, by_value, value):
        """  
        根据value进行选择
        by      定位方式
        by_value   定位值   
        index      下标    
        """
        select = self.driver.find_element(by, by_value)
        depts = Select(select)
        depts.select_by_value(value)  # 根据下标选择，下标从0开始

    def select_by_visible_text(self, by, by_value, visible_text):
        """  
        根据value进行选择
        by      定位方式
        by_value   定位值   
        index      下标    
        """
        select = self.driver.find_element(by, by_value)
        depts = Select(select)
        depts.select_by_visible_text(visible_text)  # 根据下标选择，下标从0开始

    def find_elements(self, by, value):
        """  
        定位多个元素
        by      定位方式
        by_value   定位值   
        """
        return self.driver.find_elements(by, value)

    def close(self):
        """  
        关闭当前窗口
        """
        self.driver.quit()

    def clear(self, by, value):
        """
        清空文本框，富文本框也可以
        """
        self.driver.find_element(by, value).clear()

    def find_element_by_link_text(self, value):
        """
        精确匹配a标签的值
        """
        self.driver.find_element_by_link_text(value)

    def find_element_by_partial_link_text(self):
        """
        模糊匹配a标签的值
        """
        self.driver.find_element_by_partial_link_text()

    def alert_accept(self):
        # 关闭alert操作
        self.driver.switch_to.alert.accept()

    def switch_to_default(self):
        # 跳到最外层iframe
        self.driver.switch_to.default_content()


class BasePage:

    def __init__(self, driver: BoxDriver) -> None:
        self.driver = driver

    @staticmethod  # 静态方法
    def get_yaml(path):
        """
        解析yaml配置文件
        path    yaml文件地址
        """
        with open(path, 'r', encoding='utf-8') as file:
            content = file.read()
            return yaml.safe_load(content)

    @staticmethod
    def get_excel(path, sheetname):
        """
        解析excel数据文件
        path    文件地址
        """
        # 加载工作簿
        workbook = openpyxl.load_workbook(path)
        # 获取工作簿表
        worksheet = workbook[sheetname]
        # [('user1','123456','user1'),('user2','123456','user2')]
        return list(worksheet.values)[1:]

    @staticmethod
    def send_mail(path, server, sender, code, receivers, simple, content):
        """
          path 附件路径
          sender 发件人
          code 授权码
          receivers 收件人
          server 设置邮件服务器地址
          simple 主题
          content 生成正文
        """
        # 服务器端口号 默认25
        port = 25
        """ 创建邮件 """
        # 创建邮件对象
        mail = MIMEMultipart()
        # 添加发件人
        mail['from'] = sender
        # 添加收件人
        mail['to'] = receivers
        # 添加主题
        mail['subject'] = simple

        """ 添加附件 """
        # 读取附件
        with open(path, 'rb') as file:
            report = file.read()
        # 对附件进行编码
        attchment = MIMEText(report, 'base64', 'utf-8')
        # 设置附件的类型
        attchment['Content-Type'] = 'application/octet-stream'
        # 设置附件的处理方式
        attchment['Content-Disposition'] = 'attachment;filename=%s' % path.split('/')[-1]
        # 添加附件
        mail.attach(attchment)
        """ 添加正文 """
        # 对正文进行编码
        body = MIMEText(content, 'html', 'utf-8')
        # 添加正文
        mail.attach(body)
        """ 发送邮件 """
        # 创建smtp对象
        smtp = smtplib.SMTP()
        # 连接服务器
        smtp.connect(server, port)
        # 登陆服务器
        smtp.login(sender, code)
        # 发送邮件
        smtp.sendmail(sender, receivers.split(';'), mail.as_string())
        # 关闭服务器
        smtp.close()
        print('邮件发送完毕！')
